package leetcode.offer;

import entity.TreeNode;
import org.apache.poi.ss.formula.functions.T;
import org.junit.Test;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 2021/11/24 8:43
 */
public class offer55I {

    /**
     * 输入一棵二叉树的根节点，求该树的深度。从根节点到叶节点依次经过的节点（含根、叶节点）形成树的一条路径，最长路径的长度为树的深度。
     */

    @Test
    public void test() {
        TreeNode treeNode = new TreeNode(3, new TreeNode(9, null, null), new TreeNode(20, new TreeNode(15, null, null), new TreeNode(7, null, null)));
        System.out.println(maxDepth(treeNode));
    }

    /**
     * 解法 1
     * @param root
     * @return
     */
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left, right) + 1;
    }


    public int maxDepthTwo(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left, right) + 1;
    }



}
